package ar.com.quickmart.connectivity;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import ar.com.quickmart.classes.Branch;
import ar.com.quickmart.classes.Supermarket;

public class WebServiceSupermarket extends WebServiceBase {

	public static List<Supermarket> getSuperMarketList() throws Exception {

		// Variables propias del webservice
		String methodName = "getSuperMarketList";
		Object callResult;
	
		// Variables
		List<Supermarket> supermarketsList = new ArrayList<Supermarket>();
	    
        try {
        	
        	// Obtengo el resultado
        	callResult = callWebService(methodName);
        	
        	// Si obtuve la lista
        	if(callResult != null) {
        		
        		// Hago cast
        		// FIXME ver si esto es seguro !!!!!!!!!!!!!!!
        		List<SoapObject> result = (List<SoapObject>) callResult;
        		
        		// Parseo y agrego cada elemento a la lista
        		for(SoapObject item : result) {
        			
        			long id = Long.parseLong(item.getProperty("id").toString());
        			String name = item.getProperty("name").toString();
        			String url = item.getProperty("url").toString();
        			
        			supermarketsList.add(new Supermarket(id, name, url));
        			       				
        		}
        	}
        }
        catch (Exception ex) {
        	ex.printStackTrace();
        	
        	throw ex;
        }
        
        return supermarketsList;
	}
	
	public static List<Branch> getBranchList(long supermarketId) throws Exception {

		// Variables propias del webservice
		String methodName = "getBranchList";
		String param1 = "supermarketId";
		Object callResult;
	
		// Variables
		List<Branch> branchesList = new ArrayList<Branch>();
	
		// Creo parametro con el id del supermercado
        PropertyInfo supermarketParameter = new PropertyInfo();
        supermarketParameter.setName(param1);
        supermarketParameter.setValue(supermarketId);
        supermarketParameter.setType(long.class);
		
        try {
        	
        	// Obtengo el resultado
        	callResult = callWebService(methodName, supermarketParameter);
        	
        	// Si obtuve la lista
        	if(callResult != null) {
        		
        		// Hago cast
        		// FIXME ver si esto es seguro !!!!!!!!!!!!!!!
        		List<SoapObject> result = (List<SoapObject>) callResult;
        		
        		// Parseo y agrego cada elemento a la lista
        		for(SoapObject item : result) {
        			
        			long id = Long.parseLong(item.getProperty("id").toString());        			        			
        			String address = item.getProperty("address").toString();
        			String country = item.getProperty("country").toString();
        			String province = item.getProperty("province").toString();
        			
        			branchesList.add(new Branch(id, address, country, province));        			       				
        		}
        	}
        }
        catch (Exception ex) {
        	ex.printStackTrace();
        	
        	throw ex;
        }
        
        return branchesList;
	}

	public static boolean associateListToSupermarket(String username, String listname, long branchId) throws Exception {
		// Variables propias del webservice
		String methodName = "asociateListToSuper";
		String param1 = "userName";
		String param2 = "listName";
		String param3 = "branchId";
		Object callResult;
		boolean result = false;
		
		// Creo parametro con el codigo de barras user
        PropertyInfo usernameParameter = new PropertyInfo();
        usernameParameter.setName(param1);
        usernameParameter.setValue(username);
        usernameParameter.setType(String.class);
        
        PropertyInfo listnameParameter = new PropertyInfo();
        listnameParameter.setName(param2);
        listnameParameter.setValue(listname);
        listnameParameter.setType(String.class);
        
        PropertyInfo branchIdParameter = new PropertyInfo();
        branchIdParameter.setName(param3);
        branchIdParameter.setValue(branchId);
        branchIdParameter.setType(long.class);
	    
        try {       	
        	// Obtengo el resultado y lo convierto a string
        	callResult = callWebService(methodName, usernameParameter, listnameParameter, branchIdParameter);
        	if(callResult != null) {
        		result = Boolean.parseBoolean(callResult.toString());
        	}
        }
        catch (Exception ex) {
        	ex.printStackTrace();
        	
        	throw ex;
        }      
        return result;
	}
	
}
